package com.bz.common;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

public class ExprotCellStyle {
    
    /**
     * 得到表头样式
     * 
     * @param workbook
     * @return
     */
    public static HSSFCellStyle createTableTitleStyle(HSSFWorkbook workbook) {
        HSSFCellStyle cellStyle = setRowCellCenter(workbook);// 水平居中
        // 设置字体
        HSSFFont font = setFontCellStyle(workbook, (short) 15, HSSFColorPredefined.BLUE.getIndex(), true, false,
                HSSFFont.U_NONE);
        font.setFontName("华文琥珀");
        cellStyle.setFont(font);
        return cellStyle;
    }

    /**
     * 创建小标题的样式
     * 
     * @param workbook
     * @return
     */
    public static HSSFCellStyle createSecondTitleStyle(HSSFWorkbook workbook) {
        HSSFCellStyle cellStyle = setRowCellCenter(workbook);// 水平居中
        // 设置字体
        HSSFFont font = setFontCellStyle(workbook, (short) 18, HSSFColorPredefined.GOLD.getIndex(), true, false,
                HSSFFont.U_NONE);
        cellStyle.setFont(font);
        return cellStyle;
    }

    /**
     * 创建表头的样式
     * 
     * @param workbook
     * @return
     */
    public static HSSFCellStyle createTitleCellStyle(HSSFWorkbook workbook) {
        HSSFCellStyle cellStyle = setRowCellCenter(workbook);
        setBorder(cellStyle);
        // 设置字体
        HSSFFont font = setFontCellStyle(workbook, (short) 10, HSSFColorPredefined.BLACK.getIndex(), true, false,
                HSSFFont.U_DOUBLE);
        cellStyle.setFont(font);
        HSSFDataFormat format = workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat("@"));
        return cellStyle;
    }

    /**
     * 
     * @param workbook   工作簿
     * @param fontSize   字体大小
     * @param colorIndex 字体颜色 @see HSSFColorPredefined
     * @param bold       是否加粗
     * @param italic     是否斜体
     * @param undderLine 下划线风格 @see HSSFFont.U_DOUBLE
     * @return
     */
    public static HSSFFont setFontCellStyle(HSSFWorkbook workbook, short fontSize, short colorIndex, boolean bold,
            boolean italic, byte undderLine) {
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints(fontSize);// 字体大小
        font.setColor(colorIndex);// 设置字体颜色
        font.setBold(bold);// 加粗
        font.setItalic(italic);// 设置非斜体
//        font.setUnderline(undderLine);// 设置下划线
        return font;
    }

    /**
     * 创建水平和垂直居 的方法
     * 
     * @param workbook
     * @return
     */
    public static HSSFCellStyle setRowCellCenter(HSSFWorkbook workbook) {
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        setBorder(cellStyle);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
        HSSFDataFormat format = workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat("@"));
        return cellStyle;
    }

    public static void setBorder(HSSFCellStyle cellStyle) {
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);
        cellStyle.setBorderRight(BorderStyle.MEDIUM);
        cellStyle.setBorderTop(BorderStyle.MEDIUM);
    }

    public static void setRowData(String[] rowDate, HSSFRow row, HSSFCellStyle tableBodyStyle, int offset) {
        for (int i = 0; i < rowDate.length; i++) {
            // 创建idcell
            HSSFCell idCell = row.createCell(i + offset);
            idCell.setCellValue(rowDate[i]);

            idCell.setCellStyle(tableBodyStyle);
        }
    }
    public static void setRowData(String rowDate, HSSFRow row, HSSFCellStyle tableBodyStyle, int offset) {
        // 创建idcell
        HSSFCell idCell = row.createCell(offset);
        idCell.setCellValue(rowDate);
        idCell.setCellStyle(tableBodyStyle);
    }
    
    /**
     * 添加边框
     * @param cra
     * @param sheet
     */
    public static void addMergedRegionBorder(int a,int b,int c,int d,HSSFSheet sheet) {
        CellRangeAddress cra = new CellRangeAddress(a,b,c,d);
        RegionUtil.setBorderBottom(BorderStyle.MEDIUM, cra, sheet); // 下边框
        RegionUtil.setBorderLeft(BorderStyle.MEDIUM, cra, sheet); // 左边框
        RegionUtil.setBorderRight(BorderStyle.MEDIUM, cra, sheet); // 有边框
        RegionUtil.setBorderTop(BorderStyle.MEDIUM, cra, sheet); // 上边框
        sheet.addMergedRegion(cra);
    }
}